{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11c75205-0aa2-41b3-8387-804f2db9d064",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:00.150093Z",
     "iopub.status.busy": "2022-04-26T01:45:00.149800Z",
     "iopub.status.idle": "2022-04-26T01:45:00.439653Z",
     "shell.execute_reply": "2022-04-26T01:45:00.438893Z",
     "shell.execute_reply.started": "2022-04-26T01:45:00.150025Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14569111-a398-455b-a82b-0cdfeb1dc942",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:03.843807Z",
     "iopub.status.busy": "2022-04-26T01:45:03.843559Z",
     "iopub.status.idle": "2022-04-26T01:45:03.857556Z",
     "shell.execute_reply": "2022-04-26T01:45:03.856725Z",
     "shell.execute_reply.started": "2022-04-26T01:45:03.843782Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 读取csv\n",
    "data = pd.read_csv(r\"data/lianjia.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c676268-5365-4be7-a571-974d53ba6b58",
   "metadata": {},
   "source": [
    "## 1. 观察结构，调整列索引顺序\n",
    "\n",
    "（Region, Garden, Layout, Floor, ID, Year, Size, Elevator, Direction, Renovation, Price）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "00407144-6bb8-47f6-ab86-efe003a73808",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:07.061285Z",
     "iopub.status.busy": "2022-04-26T01:45:07.061042Z",
     "iopub.status.idle": "2022-04-26T01:45:07.083378Z",
     "shell.execute_reply": "2022-04-26T01:45:07.082649Z",
     "shell.execute_reply.started": "2022-04-26T01:45:07.061260Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Direction</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Id</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Price</th>\n",
       "      <th>Region</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Size</th>\n",
       "      <th>Year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>南北</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>翠竹苑</td>\n",
       "      <td>0</td>\n",
       "      <td>3室1厅</td>\n",
       "      <td>365.0</td>\n",
       "      <td>浦东</td>\n",
       "      <td>简装</td>\n",
       "      <td>77.84</td>\n",
       "      <td>1995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>南</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>低楼层(共36层)</td>\n",
       "      <td>百汇园</td>\n",
       "      <td>1</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>1449.0</td>\n",
       "      <td>徐汇</td>\n",
       "      <td>精装</td>\n",
       "      <td>145.2</td>\n",
       "      <td>1995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>南北</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>仁恒河滨城(二期)</td>\n",
       "      <td>2</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>1630.0</td>\n",
       "      <td>浦东</td>\n",
       "      <td>精装</td>\n",
       "      <td>161.94</td>\n",
       "      <td>1995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>南</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>高楼层(共30层)</td>\n",
       "      <td>财富海景花园</td>\n",
       "      <td>3</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>浦东</td>\n",
       "      <td>精装</td>\n",
       "      <td>185</td>\n",
       "      <td>1995.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>东</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>仁恒滨江园</td>\n",
       "      <td>4</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>1360.0</td>\n",
       "      <td>浦东</td>\n",
       "      <td>精装</td>\n",
       "      <td>130.41</td>\n",
       "      <td>1995.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Direction Elevator      Floor     Garden  Id Layout   Price Region  \\\n",
       "0        南北      无电梯   低楼层(共6层)        翠竹苑   0   3室1厅   365.0     浦东   \n",
       "1         南      有电梯  低楼层(共36层)        百汇园   1   3室2厅  1449.0     徐汇   \n",
       "2        南北      有电梯  中楼层(共26层)  仁恒河滨城(二期)   2   3室2厅  1630.0     浦东   \n",
       "3         南      有电梯  高楼层(共30层)     财富海景花园   3   3室2厅  2000.0     浦东   \n",
       "4         东      有电梯  中楼层(共26层)      仁恒滨江园   4   3室2厅  1360.0     浦东   \n",
       "\n",
       "  Renovation    Size    Year  \n",
       "0         简装   77.84  1995.0  \n",
       "1         精装   145.2  1995.0  \n",
       "2         精装  161.94  1995.0  \n",
       "3         精装     185  1995.0  \n",
       "4         精装  130.41  1995.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看结构\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "916fca91-b8a5-4a0d-ac42-bafa1e407a38",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:20.444515Z",
     "iopub.status.busy": "2022-04-26T01:45:20.444222Z",
     "iopub.status.idle": "2022-04-26T01:45:20.465228Z",
     "shell.execute_reply": "2022-04-26T01:45:20.464138Z",
     "shell.execute_reply.started": "2022-04-26T01:45:20.444486Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Id</th>\n",
       "      <th>Year</th>\n",
       "      <th>Size</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>浦东</td>\n",
       "      <td>翠竹苑</td>\n",
       "      <td>3室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>77.84</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>简装</td>\n",
       "      <td>365.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>百汇园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>低楼层(共36层)</td>\n",
       "      <td>1</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>145.2</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>1449.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒河滨城(二期)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>2</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>161.94</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>精装</td>\n",
       "      <td>1630.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>浦东</td>\n",
       "      <td>财富海景花园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>高楼层(共30层)</td>\n",
       "      <td>3</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>185</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>2000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒滨江园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>4</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>130.41</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>东</td>\n",
       "      <td>精装</td>\n",
       "      <td>1360.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1705</th>\n",
       "      <td>杨浦</td>\n",
       "      <td>水电新村</td>\n",
       "      <td>1室2厅</td>\n",
       "      <td>高楼层(共6层)</td>\n",
       "      <td>1705</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>57.62</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>260.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1706</th>\n",
       "      <td>浦东</td>\n",
       "      <td>祥安菊苑</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共11层)</td>\n",
       "      <td>1706</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>126.58</td>\n",
       "      <td>NaN</td>\n",
       "      <td>南</td>\n",
       "      <td>其他</td>\n",
       "      <td>250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1707</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>梅陇四村</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>1707</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>64.58</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>其他</td>\n",
       "      <td>320.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1708</th>\n",
       "      <td>嘉定</td>\n",
       "      <td>翔和雅苑</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>高楼层(共18层)</td>\n",
       "      <td>1708</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>69.6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>南</td>\n",
       "      <td>毛坯</td>\n",
       "      <td>260.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1709</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>华泾绿苑</td>\n",
       "      <td>1室1厅</td>\n",
       "      <td>低楼层(共11层)</td>\n",
       "      <td>1709</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>64.56</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>毛坯</td>\n",
       "      <td>310.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1710 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Region     Garden Layout      Floor    Id    Year    Size Elevator  \\\n",
       "0        浦东        翠竹苑   3室1厅   低楼层(共6层)     0  1995.0   77.84      无电梯   \n",
       "1        徐汇        百汇园   3室2厅  低楼层(共36层)     1  1995.0   145.2      有电梯   \n",
       "2        浦东  仁恒河滨城(二期)   3室2厅  中楼层(共26层)     2  1995.0  161.94      有电梯   \n",
       "3        浦东     财富海景花园   3室2厅  高楼层(共30层)     3  1995.0     185      有电梯   \n",
       "4        浦东      仁恒滨江园   3室2厅  中楼层(共26层)     4  1995.0  130.41      有电梯   \n",
       "...     ...        ...    ...        ...   ...     ...     ...      ...   \n",
       "1705     杨浦       水电新村   1室2厅   高楼层(共6层)  1705  1995.0   57.62      无电梯   \n",
       "1706     浦东       祥安菊苑   3室2厅  中楼层(共11层)  1706  1995.0  126.58      NaN   \n",
       "1707     徐汇       梅陇四村   2室1厅   低楼层(共6层)  1707  1995.0   64.58      无电梯   \n",
       "1708     嘉定       翔和雅苑   2室1厅  高楼层(共18层)  1708  1995.0    69.6      NaN   \n",
       "1709     徐汇       华泾绿苑   1室1厅  低楼层(共11层)  1709  1995.0   64.56      有电梯   \n",
       "\n",
       "     Direction Renovation   Price  \n",
       "0           南北         简装   365.0  \n",
       "1            南         精装  1449.0  \n",
       "2           南北         精装  1630.0  \n",
       "3            南         精装  2000.0  \n",
       "4            东         精装  1360.0  \n",
       "...        ...        ...     ...  \n",
       "1705         南         精装   260.0  \n",
       "1706         南         其他   250.0  \n",
       "1707        南北         其他   320.0  \n",
       "1708         南         毛坯   260.0  \n",
       "1709         南         毛坯   310.0  \n",
       "\n",
       "[1710 rows x 11 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调整列索引顺序\n",
    "data = data.reindex(\n",
    "    columns=[\n",
    "        \"Region\",\n",
    "        \"Garden\",\n",
    "        \"Layout\",\n",
    "        \"Floor\",\n",
    "        \"Id\",\n",
    "        \"Year\",\n",
    "        \"Size\",\n",
    "        \"Elevator\",\n",
    "        \"Direction\",\n",
    "        \"Renovation\",\n",
    "        \"Price\",\n",
    "    ]\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5004c9ba-c7b3-47b1-b79c-e64ee2af0f39",
   "metadata": {},
   "source": [
    "## 2. 增加一列关于目前状况\n",
    "（状况：state，可以使用1代表售出，0代表未售出）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03701dd9-6392-4bcb-b7e5-579e63b92932",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:28.728626Z",
     "iopub.status.busy": "2022-04-26T01:45:28.728381Z",
     "iopub.status.idle": "2022-04-26T01:45:28.745211Z",
     "shell.execute_reply": "2022-04-26T01:45:28.744183Z",
     "shell.execute_reply.started": "2022-04-26T01:45:28.728600Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There is 1710 rows in data.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Id</th>\n",
       "      <th>Year</th>\n",
       "      <th>Size</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Price</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>浦东</td>\n",
       "      <td>翠竹苑</td>\n",
       "      <td>3室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>0</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>77.84</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>简装</td>\n",
       "      <td>365.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>百汇园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>低楼层(共36层)</td>\n",
       "      <td>1</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>145.2</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>1449.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒河滨城(二期)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>2</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>161.94</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>精装</td>\n",
       "      <td>1630.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>浦东</td>\n",
       "      <td>财富海景花园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>高楼层(共30层)</td>\n",
       "      <td>3</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>185</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒滨江园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>4</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>130.41</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>东</td>\n",
       "      <td>精装</td>\n",
       "      <td>1360.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Region     Garden Layout      Floor  Id    Year    Size Elevator Direction  \\\n",
       "0     浦东        翠竹苑   3室1厅   低楼层(共6层)   0  1995.0   77.84      无电梯        南北   \n",
       "1     徐汇        百汇园   3室2厅  低楼层(共36层)   1  1995.0   145.2      有电梯         南   \n",
       "2     浦东  仁恒河滨城(二期)   3室2厅  中楼层(共26层)   2  1995.0  161.94      有电梯        南北   \n",
       "3     浦东     财富海景花园   3室2厅  高楼层(共30层)   3  1995.0     185      有电梯         南   \n",
       "4     浦东      仁恒滨江园   3室2厅  中楼层(共26层)   4  1995.0  130.41      有电梯         东   \n",
       "\n",
       "  Renovation   Price  State  \n",
       "0         简装   365.0      1  \n",
       "1         精装  1449.0      1  \n",
       "2         精装  1630.0      0  \n",
       "3         精装  2000.0      0  \n",
       "4         精装  1360.0      1  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取data行数\n",
    "rowNumber = data.shape[0]\n",
    "print(\"There is {} rows in data.\".format(rowNumber))\n",
    "\n",
    "# 生成0和1的1维数组\n",
    "states = np.random.randint(0, 2, size=(rowNumber,))\n",
    "\n",
    "# 给data增加State列\n",
    "data[\"State\"] = states\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4a1440d-618b-461a-a23d-7a6f589bf3d1",
   "metadata": {},
   "source": [
    "## 3. 删除ID这一列无用的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e3e8b72c-8a08-4607-88d3-ca1785beecaf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:36.238809Z",
     "iopub.status.busy": "2022-04-26T01:45:36.238296Z",
     "iopub.status.idle": "2022-04-26T01:45:36.254258Z",
     "shell.execute_reply": "2022-04-26T01:45:36.253485Z",
     "shell.execute_reply.started": "2022-04-26T01:45:36.238753Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Year</th>\n",
       "      <th>Size</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Price</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>浦东</td>\n",
       "      <td>翠竹苑</td>\n",
       "      <td>3室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>77.84</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>简装</td>\n",
       "      <td>365.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>百汇园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>低楼层(共36层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>145.2</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>1449.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒河滨城(二期)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>161.94</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>精装</td>\n",
       "      <td>1630.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>浦东</td>\n",
       "      <td>财富海景花园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>高楼层(共30层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>185</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒滨江园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>130.41</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>东</td>\n",
       "      <td>精装</td>\n",
       "      <td>1360.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Region     Garden Layout      Floor    Year    Size Elevator Direction  \\\n",
       "0     浦东        翠竹苑   3室1厅   低楼层(共6层)  1995.0   77.84      无电梯        南北   \n",
       "1     徐汇        百汇园   3室2厅  低楼层(共36层)  1995.0   145.2      有电梯         南   \n",
       "2     浦东  仁恒河滨城(二期)   3室2厅  中楼层(共26层)  1995.0  161.94      有电梯        南北   \n",
       "3     浦东     财富海景花园   3室2厅  高楼层(共30层)  1995.0     185      有电梯         南   \n",
       "4     浦东      仁恒滨江园   3室2厅  中楼层(共26层)  1995.0  130.41      有电梯         东   \n",
       "\n",
       "  Renovation   Price  State  \n",
       "0         简装   365.0      1  \n",
       "1         精装  1449.0      1  \n",
       "2         精装  1630.0      0  \n",
       "3         精装  2000.0      0  \n",
       "4         精装  1360.0      1  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除ID列，直接操作原数据\n",
    "data.drop(\"Id\", axis=1, inplace=True)  # 也可以写成 data.drop(\"Id\", axis=\"columns\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa33c4ec-ee93-4aba-b76f-84838acd56c2",
   "metadata": {},
   "source": [
    "## 4. 查找楼层（floor）小于7的所有房屋"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f8e6410a-645f-4b35-98b6-68c1622fd72f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:45:55.643335Z",
     "iopub.status.busy": "2022-04-26T01:45:55.643076Z",
     "iopub.status.idle": "2022-04-26T01:45:55.662964Z",
     "shell.execute_reply": "2022-04-26T01:45:55.661904Z",
     "shell.execute_reply.started": "2022-04-26T01:45:55.643308Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Year</th>\n",
       "      <th>Size</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Price</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>闵行</td>\n",
       "      <td>绿野香洲(公寓)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共5层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>107.53</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>660.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>杨浦</td>\n",
       "      <td>安图新村</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>中楼层(共5层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>58.05</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>320.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>松江</td>\n",
       "      <td>万科白马花园(公寓)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共5层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>88.29</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>312.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>浦东</td>\n",
       "      <td>万科蓝山</td>\n",
       "      <td>联排别墅</td>\n",
       "      <td>3层2005年建板楼)</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>NaN</td>\n",
       "      <td>223.79平米</td>\n",
       "      <td>南</td>\n",
       "      <td>1180.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>谨斜小区</td>\n",
       "      <td>1室1厅</td>\n",
       "      <td>中楼层(共5层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>34.62</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>220.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Region      Garden Layout        Floor    Year    Size Elevator Direction  \\\n",
       "11     闵行    绿野香洲(公寓)   3室2厅     中楼层(共5层)  1995.0  107.53      无电梯         南   \n",
       "27     杨浦        安图新村   2室1厅     中楼层(共5层)  1995.0   58.05      无电梯         南   \n",
       "64     松江  万科白马花园(公寓)   3室2厅     中楼层(共5层)  1995.0   88.29      无电梯         南   \n",
       "78     浦东        万科蓝山   联排别墅  3层2005年建板楼)     NaN    3室2厅      NaN  223.79平米   \n",
       "89     徐汇        谨斜小区   1室1厅     中楼层(共5层)  1995.0   34.62      无电梯         南   \n",
       "\n",
       "   Renovation   Price  State  \n",
       "11         精装   660.0      1  \n",
       "27         精装   320.0      0  \n",
       "64         精装   312.0      0  \n",
       "78          南  1180.0      0  \n",
       "89         精装   220.0      0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 写字符串处理函数\n",
    "def getFloorNumber(floorInf):\n",
    "    start = floorInf.find(\"共\") + 1\n",
    "    stop = floorInf.rfind(\"层\")\n",
    "    return int(floorInf[start:stop])\n",
    "\n",
    "\n",
    "# 写个判断函数，生成布尔值\n",
    "def isLowFloor(floor):\n",
    "    isLow = []\n",
    "    for i in floor:\n",
    "        floorNumbers = getFloorNumber(i)\n",
    "        if floorNumbers < 6:\n",
    "            isLow.append(True)\n",
    "        else:\n",
    "            isLow.append(False)\n",
    "    return isLow\n",
    "\n",
    "\n",
    "lowFloor = data[isLowFloor(data[\"Floor\"])]\n",
    "lowFloor.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da0a4baa-dfc3-41ef-9b17-ae4697815363",
   "metadata": {},
   "source": [
    "## 5. 修改楼层小于7的房源的电梯状态，统一改成无电梯，反之就设为有电梯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "41b2298d-152a-407c-b608-9da8b9b0ebd9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-26T01:46:03.898154Z",
     "iopub.status.busy": "2022-04-26T01:46:03.897908Z",
     "iopub.status.idle": "2022-04-26T01:46:03.915444Z",
     "shell.execute_reply": "2022-04-26T01:46:03.914875Z",
     "shell.execute_reply.started": "2022-04-26T01:46:03.898128Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th>Garden</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Year</th>\n",
       "      <th>Size</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Price</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>浦东</td>\n",
       "      <td>翠竹苑</td>\n",
       "      <td>3室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>77.84</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>简装</td>\n",
       "      <td>365.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>百汇园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>低楼层(共36层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>145.2</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>1449.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒河滨城(二期)</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>161.94</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>精装</td>\n",
       "      <td>1630.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>浦东</td>\n",
       "      <td>财富海景花园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>高楼层(共30层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>185</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浦东</td>\n",
       "      <td>仁恒滨江园</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共26层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>130.41</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>东</td>\n",
       "      <td>精装</td>\n",
       "      <td>1360.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1705</th>\n",
       "      <td>杨浦</td>\n",
       "      <td>水电新村</td>\n",
       "      <td>1室2厅</td>\n",
       "      <td>高楼层(共6层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>57.62</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>260.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1706</th>\n",
       "      <td>浦东</td>\n",
       "      <td>祥安菊苑</td>\n",
       "      <td>3室2厅</td>\n",
       "      <td>中楼层(共11层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>126.58</td>\n",
       "      <td>NaN</td>\n",
       "      <td>南</td>\n",
       "      <td>其他</td>\n",
       "      <td>250.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1707</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>梅陇四村</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>64.58</td>\n",
       "      <td>无电梯</td>\n",
       "      <td>南北</td>\n",
       "      <td>其他</td>\n",
       "      <td>320.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1708</th>\n",
       "      <td>嘉定</td>\n",
       "      <td>翔和雅苑</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>高楼层(共18层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>69.6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>南</td>\n",
       "      <td>毛坯</td>\n",
       "      <td>260.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1709</th>\n",
       "      <td>徐汇</td>\n",
       "      <td>华泾绿苑</td>\n",
       "      <td>1室1厅</td>\n",
       "      <td>低楼层(共11层)</td>\n",
       "      <td>1995.0</td>\n",
       "      <td>64.56</td>\n",
       "      <td>有电梯</td>\n",
       "      <td>南</td>\n",
       "      <td>毛坯</td>\n",
       "      <td>310.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1710 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Region     Garden Layout      Floor    Year    Size Elevator Direction  \\\n",
       "0        浦东        翠竹苑   3室1厅   低楼层(共6层)  1995.0   77.84      无电梯        南北   \n",
       "1        徐汇        百汇园   3室2厅  低楼层(共36层)  1995.0   145.2      有电梯         南   \n",
       "2        浦东  仁恒河滨城(二期)   3室2厅  中楼层(共26层)  1995.0  161.94      有电梯        南北   \n",
       "3        浦东     财富海景花园   3室2厅  高楼层(共30层)  1995.0     185      有电梯         南   \n",
       "4        浦东      仁恒滨江园   3室2厅  中楼层(共26层)  1995.0  130.41      有电梯         东   \n",
       "...     ...        ...    ...        ...     ...     ...      ...       ...   \n",
       "1705     杨浦       水电新村   1室2厅   高楼层(共6层)  1995.0   57.62      无电梯         南   \n",
       "1706     浦东       祥安菊苑   3室2厅  中楼层(共11层)  1995.0  126.58      NaN         南   \n",
       "1707     徐汇       梅陇四村   2室1厅   低楼层(共6层)  1995.0   64.58      无电梯        南北   \n",
       "1708     嘉定       翔和雅苑   2室1厅  高楼层(共18层)  1995.0    69.6      NaN         南   \n",
       "1709     徐汇       华泾绿苑   1室1厅  低楼层(共11层)  1995.0   64.56      有电梯         南   \n",
       "\n",
       "     Renovation   Price  State  \n",
       "0            简装   365.0      1  \n",
       "1            精装  1449.0      1  \n",
       "2            精装  1630.0      0  \n",
       "3            精装  2000.0      0  \n",
       "4            精装  1360.0      1  \n",
       "...         ...     ...    ...  \n",
       "1705         精装   260.0      1  \n",
       "1706         其他   250.0      1  \n",
       "1707         其他   320.0      0  \n",
       "1708         毛坯   260.0      0  \n",
       "1709         毛坯   310.0      0  \n",
       "\n",
       "[1710 rows x 11 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用标签索引修改\n",
    "data.loc[lowFloor.index, \"Elevator\"] = \"无电梯\"\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
